<template>
  <div>
    <el-row type="flex" justify="center">
      <!--  给el-form的动态绑定ruleForm-->
      <el-form
        :model="ruleForm"
        :inline="true"
        status-icon
        :rules="rules"
        ref="ruleForm"
      >
        <el-form-item prop="id" label="文章ID">
          <el-col :span="24"
            ><el-input type="text" v-model.number="ruleForm.id" ></el-input>
          </el-col>
        </el-form-item>
        <el-form-item prop="path" label="文章Path">
          <el-col :span="24"
            ><el-input type="text" v-model="ruleForm.path"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item prop="title" label="文章Title">
          <el-col :span="24"
            ><el-input type="text" v-model="ruleForm.title"></el-input>
          </el-col>
        </el-form-item>
      </el-form>
      <!-- 上传图片 -->
 
    </el-row>
    <el-row type="flex" justify="center">
      <el-col :span="12">
 <el-upload
  class="upload-demo"
  action="http://localhost:8030/index/uploads"
  :on-preview="handlePreview"
  :on-remove="handleRemove"
  :on-success="succ"
  list-type="picture">
  <!-- list-type图片预览效果 -->
  <el-button size="small" type="primary">点击上传</el-button>
</el-upload>
      </el-col>
    </el-row>
    <!-- markDown语法解析 -->
    <mavon-editor
      class="md"
      :value="ruleForm.preFace"
      :subfield="prop.subfield"
      :defaultOpen="prop.defaultOpen"
      :toolbarsFlag="prop.toolbarsFlag"
      :editable="prop.editable"
      :scrollStyle="prop.scrollStyle"
    />
    <!-- 文本编辑区 -->
    <mavon-editor
      v-model="ruleForm.preFace"
      :toolbars="toolbars"
      class="md"
    ></mavon-editor>
    <el-row type="flex" justify="center">
      <el-col :span="12"><el-button @click="addEssay" type="primary">提交</el-button></el-col>
    </el-row>
    <!-- 图片预览 -->
    <el-dialog
  title="图片预览"
  :visible.sync="previewVisible"
  width="50%">
 
  <img :src="previewPath" alt="" class="previewImg">
</el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    let validatePass = (rule, value, callback) => {
      if (value && typeof value === "number" && String(value).length == 1) {
        callback();
      } else {
        callback(
          this.$message({
            message: "请输入0-8的任意一个数字",
            center: true,
            type: "error",
            showClose: true,
          })
        );
      }
    };
    let validatePass1 = (rule, value, callback) => {
      //定义正则
      let reg = /^\/[a-z]{1,}\/[0-9]{1,}$/g;

      if (reg.test(value)) {
        callback();
      } else {
        callback(
          this.$message({
            message: "请输入类似java/12形式的路径",
            center: true,
            type: "error",
            showClose: true,
          })
        );
      }
    };
    let validatePass2 = (rule, value, callback) => {
      callback();
    };

    return {
      toolbars: {
        bold: true, // 粗体
        italic: true, // 斜体
        header: true, // 标题
        underline: true, // 下划线
        strikethrough: true, // 中划线
        mark: true, // 标记
        superscript: true, // 上角标
        subscript: true, // 下角标
        quote: true, // 引用
        ol: true, // 有序列表
        ul: true, // 无序列表
        link: true, // 链接
        imagelink: true, // 图片链接
        code: true, // code
        table: true, // 表格
        fullscreen: true, // 全屏编辑
        readmodel: true, // 沉浸式阅读
        htmlcode: true, // 展示html源码
        help: true, // 帮助
        /* 1.3.5 */
        undo: true, // 上一步
        redo: true, // 下一步
        trash: true, // 清空
        save: true, // 保存（触发events中的save事件）
        /* 1.4.2 */
        navigation: true, // 导航目录
        /* 2.1.8 */
        alignleft: true, // 左对齐
        aligncenter: true, // 居中
        alignright: true, // 右对齐
        /* 2.2.1 */
        subfield: true, // 单双栏模式
        preview: true, // 预览
      },
      fileList:[{naem:'',url:''}],
      previewPath:'',
      previewVisible:false,//默认隐藏对话框
      ruleForm: {
        id: "",
        path: "",
        imgSrc:'',
        title: "",
        preFace: ""
      },
      rules: {
        id: [{ validator: validatePass, trigger: "blur" }],
        path: [
          {
            validator: validatePass1,
            trigger: "blur",
          },
        ],
        title: [{ validator: validatePass2, trigger: "blur" }],
      },
    };
  },
  computed: {
    // 解析器配置
    prop() {
      let data = {
        subfield: false, // 单双栏模式
        defaultOpen: "preview", //edit： 默认展示编辑区域 ， preview： 默认展示预览区域
        editable: false, // 是否允许编辑
        toolbarsFlag: false,
        scrollStyle: true,
      };
      return data;
    },
  },
  created() {
    this.ruleForm.pass = this.value;
  },
  methods:{
    //长传成功的钩子s
    succ(response, file, fileList){
    // var binaryData = [];
    // console.log(typeof response.data);
    // binaryData.push(response.data);
    // let sr=window.URL.createObjectURL(new Blob(binaryData),{type: "application/octet-binary"});
  
    this.ruleForm.imgSrc=response.data;//保存图片上传地址
      //console.log(response.data);
    },
     //文件列表移除时的钩子

     handleRemove(file, fileList) {
        console.log(file, fileList);
      },
     //图片预览
      handlePreview(file) {
        this.previewPath=file.url;
        this.previewVisible=true;
        //console.log(file,999,this.ruleForm.imgSrc);
      },
    async addEssay(){
     
    //let reg=/^"\\n$"/g;//去掉开头和结尾的“”去掉\n换行符
     
     if(this.ruleForm.id&&this.ruleForm.preFace){
    
       let {data}=await this.$http.post(
      'http://localhost:8030/index/Earticle',this.ruleForm)
      //this.preFace=data.replace(reg,'')
  
      if(data.status==1){
        this.$message({message:data.message,type:'warning'})
        this.ruleForm={
        id: "",
        path: "",
        imgSrc:'',
        title: "",
        preFace: ""
      }
      this.previewPath=null
      }else{
        this.$message({type:'success',message:data.message})
        this.ruleForm={
        id: "",
        path: "",
        imgSrc:'',
        title: "",
        preFace: ""
      }
      this.previewPath=null
      }
      console.log(data);
     }
    console.log(this.ruleForm.preFace);
     this.$message({message:`填写内容`,type:'warning'})
     return;
    }
  }
};
</script>

<style lang='less' scoped>

.el-col{
  text-align: center;
}
/deep/ .el-upload-list__item-name{
  height: 70px;
}
.previewImg{
  width: 100%;
}
</style>